package utils

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

//{
//"Name": "cherry server app",
//"Host": "127.0.0.1",
//"Port": 11111,
//"MaxConn": 1000,
//"MaxPackageSize": 4096,
//}
type GConfig struct {
	Name           string `json:"Name"`
	Host           string `json:"Host"`
	TCPPort        string `json:"Port"`
	MaxConn        int    `json:"MaxConn"`
	MaxPackageSize int    `json:"MaxPackageSize"`
	PendingSize    int	  `json:"PendingSize"`
}

var GlobalConfig *GConfig

func (g *GConfig) Load(configPath string) {
	fileData, err := ioutil.ReadFile(configPath)
	if err != nil {
		fmt.Println("Reload path = ", configPath, " err = ", err)
		panic(err)
	}
	err = json.Unmarshal(fileData, g)
	if err != nil {
		fmt.Println("Reload unmarshal file = ", fileData, " err = ", err)
		panic(err)
	}
}

func init() {
	GlobalConfig = &GConfig{}
}

func MaxPackageSize()int{
	return GlobalConfig.MaxPackageSize
}

func MaxPendingSize()int{
	return GlobalConfig.PendingSize
}